<?php
/**
 * Unfuddle API Request
 * This class represents a request to the Unfuddle API. Requests are passed to
 * the Unfuddle_Api::dispatch() method, from which an Unfuddle_Response is
 * returned.
 *
 * @package     Unfuddle
 * @subpackage  API Request
 * @author      Luke Visinoni <luke.visinoni@gmail.com>
 * @copyright   (c) 2010 Luke Visinoni <luke.visinoni@gmail.com>
 * @version     $Id: Request.php 11 2011-01-13 00:27:06Z luke.visinoni $
 */
class Unfuddle_Request extends Unfuddle_Http {

    protected $_method;
    
    protected $_uri;
    
    protected $_methods = array('GET', 'POST', 'PUT', 'DELETE');
        
    public function __construct($uri, $method = 'GET', $headers = array()) {
    
        $this->setUri($uri)
             ->setMethod($method)
             ->setHeaders($headers);
    
    }
    
    public function setUri($uri) {
    
        $this->_uri = $uri;
        return $this;
    
    }
    
    public function getUri() {
    
        return '/' . ltrim($this->_uri, '/');
    
    }
    
    public function setMethod($method) {
    
        $method = strtoupper($method);
        if (!in_array($method, $this->_methods)) {
            throw new Unfuddle_Request_Exception("{$method} is not a valid request method.");
        }
        $this->_method = $method;
        return $this;
    
    }
    
    public function getMethod() {
    
        return $this->_method;
    
    }
    
    public function __toString() {
    
        return $this->getMethod() . " " . $this->getUri() . " " . self::HTTP_VERSION ."\r\n" . $this->getHeadersAsString() . "\r\n" . $this->getBody();
    
    }
}